package goomi.member;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import db.mysql.mySqlUtils;

public class memberService {
	
	HttpServletRequest request;
	
	public memberService() {
	}
	
	public memberService(HttpServletRequest request) {
		this.request = request;
	}

	public static void main(String args[]) 
	{
		//test
		try{
//			memberService color = new memberService();
//			color.getColors();
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public HashMap login(String id, String password){
		Connection conn = null;
		try{
			HashMap tmp = new HashMap();
			tmp.put("message", "FAIL");
			request.getSession().removeAttribute("id");
			request.getSession().removeAttribute("name");
			request.getSession().removeAttribute("type");
			
			id= org.apache.commons.lang.StringEscapeUtils.escapeSql(id);
			password= org.apache.commons.lang.StringEscapeUtils.escapeSql(password);
			
			conn = mySqlUtils.getConn();
			Statement st = conn.createStatement();
			String sql = "select * from member where id='" +id+ "' and password = '" + password + "' ";
			System.out.println(sql);
			ResultSet rs = st.executeQuery(sql);
			while(rs!= null && rs.next()){
				request.getSession().setAttribute("id", id);
				request.getSession().setAttribute("name", rs.getString("name"));
				request.getSession().setAttribute("type", rs.getString("type"));
				tmp.remove("message");
				tmp.put("message", "OK");
				break;
			}
			rs.close();
			st.close();
			return tmp;
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch (Exception e) {}
			}
		}
		return null;
	}
	
	public HashMap getData(String id){
		Connection conn = null;
		try{
			HashMap tmp = new HashMap();
			tmp.put("message", "FAIL");
			
			id= org.apache.commons.lang.StringEscapeUtils.escapeSql(id);
			
			conn = mySqlUtils.getConn();
			Statement st = conn.createStatement();
			String sql = "select * from member where id='" +id+ "' ";
			System.out.println(sql);
			ArrayList ar = mySqlUtils.Query(sql);
			if(ar != null && ar.size() > 0){
				return (HashMap)ar.get(0);
			}
			
			
//			ResultSet rs = st.executeQuery(sql);
//			while(rs!= null && rs.next()){
//				request.getSession().setAttribute("id", id);
//				request.getSession().setAttribute("name", rs.getString("name"));
//				request.getSession().setAttribute("type", rs.getString("type"));
//				
//				tmp.remove("message");
//				tmp.put("message", "OK");
//				if(rs.getString("id") != null)tmp.put("id", rs.getString("id"));
//				if(rs.getString("name") != null)tmp.put("name", rs.getString("name"));
//				if(rs.getString("password") != null)tmp.put("password", rs.getString("password"));
//				if(rs.getString("email") != null)tmp.put("email", rs.getString("email"));
//				if(rs.getString("phone") != null)tmp.put("phone", rs.getString("phone"));
//				if(rs.getString("address") != null)tmp.put("address", rs.getString("address"));
//				if(rs.getString("sex") != null)tmp.put("sex", rs.getString("sex"));
//				if(rs.getString("age") != null)tmp.put("age", rs.getString("age"));
//				
//				break;
//			}
//			rs.close();
//			st.close();
			return tmp;
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch (Exception e) {}
			}
		}
		return null;
	}
	
	public ArrayList getChild(String parentId){
		Connection conn = null;
		try{
//			conn = mySqlUtils.getConn();
//			Statement st = conn.createStatement();
			String sql = "select * from child where parent_id='" + parentId + "'";
			
			ArrayList ar = mySqlUtils.Query(sql);
			if(ar != null && ar.size() > 0){
				return ar;
			}
//			ArrayList tmp = new ArrayList();
//			while(rs!= null && rs.next()){
//				HashMap tmpMap = new HashMap();
//				if(rs.getString("id")!= null)tmpMap.put("id", rs.getString("id"));
//				if(rs.getString("parent_id")!= null)tmpMap.put("parent_id", rs.getString("parent_id"));
//				if(rs.getString("name")!= null)tmpMap.put("name", rs.getString("name"));
//				if(rs.getString("weight")!= null)tmpMap.put("weight", rs.getString("weight"));
//				if(rs.getString("height")!= null)tmpMap.put("height", rs.getString("height"));
//				if(rs.getString("sex")!= null)tmpMap.put("sex", rs.getString("sex"));
//				if(rs.getString("picture_id")!= null)tmpMap.put("picture_id", rs.getString("picture_id"));
//				
//				tmp.add(tmpMap);
//			}
//			rs.close();
//			st.close();
//			return tmp;
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		return null;
	}
	
	
	public HashMap logout(String id){
		try{
			HashMap tmp = new HashMap();
			tmp.put("message", "OK");
			request.getSession().removeAttribute("id");
			request.getSession().removeAttribute("name");
			request.getSession().removeAttribute("type");
			return tmp;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public ArrayList getMember(String type){
		String sql = "select * from member ";
		if(type != null && !"all".equals(type)){
			sql += " where type = '" + type + "' ";
		}
		sql += " order by id";
		System.out.println(sql);
		return mySqlUtils.Query(sql);
			
	}
}
